package com.battcn.platform.service.money.impl;


import com.battcn.framework.mybatis.page.DataGrid;
import com.battcn.platform.mapper.money.TbExpenditureMapper;
import com.battcn.platform.mapper.money.TbExpenditureRecordMapper;
import com.battcn.platform.pojo.dto.money.ExpenditureDto;
import com.battcn.platform.pojo.dto.money.ExpenditureRecordDto;
import com.battcn.platform.pojo.message.ApiResult;
import com.battcn.platform.pojo.po.money.TbExpenditure;
import com.battcn.platform.pojo.po.money.TbExpenditureRecord;
import com.battcn.platform.service.money.ExpenditureService;

import com.battcn.platform.service.sys.impl.BaseServiceImpl;
import com.battcn.platform.util.GenIdUtil;
import com.battcn.platform.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.battcn.framework.common.exception.BattcnException.notFound;

@Service
public class ExpenditureServiceImpl extends BaseServiceImpl<TbExpenditure> implements ExpenditureService{

    private final TbExpenditureMapper tbExpenditureMapper;
    private final TbExpenditureRecordMapper tbExpenditureRecordMapper;


    @Autowired
    public ExpenditureServiceImpl(TbExpenditureMapper tbExpenditureMapper,TbExpenditureRecordMapper tbExpenditureRecordMapper) {
        this.tbExpenditureMapper = tbExpenditureMapper;
        this.tbExpenditureRecordMapper = tbExpenditureRecordMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Object key) {
        return tbExpenditureMapper.deleteById((String)key) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult<String> updateStatus(TbExpenditureRecord record) {
        if(record.getStatus().equals("6")) {
            TbExpenditure t = super.selectById(record.getExpid()).orElseThrow(() -> notFound("未找到记录"));
            if (StringUtils.isNotEmpty(t.getStatus()) && t.getStatus().equals("6")) {
                return ApiResult.getFailure("该申请已被撤回，请刷新");
            }
        }
        //修改申请表状态
        TbExpenditure expenditure = new TbExpenditure();
        expenditure.setId(record.getExpid());
        expenditure.setStatus(record.getStatus());
        super.updateSelectiveById(expenditure);

        //添加审批记录
        record.setCreateBy(SessionUtil.getSession().getId());
        record.setCreateDate(new Date());
        record.setUpdateBy(SessionUtil.getSession().getId());
        record.setUpdateDate(new Date());
        record.setId(GenIdUtil.uuid());
        return ApiResult.getResponse(this.tbExpenditureRecordMapper.insert(record) > 0);
    }


    public List<ExpenditureRecordDto> findRecordList(ExpenditureRecordDto dto){
        return tbExpenditureRecordMapper.findRecordList(dto);
    }

    public PageInfo<ExpenditureDto> findRecordPage(DataGrid grid,ExpenditureDto expenditure){
        grid.getOrderBy();
        PageInfo<ExpenditureDto> page = this.findList(grid,expenditure);

        if(page.getList() != null && page.getList().size() > 0){
            List<String> ids = page.getList().stream().map(TbExpenditure :: getId).collect(Collectors.toList());
            ExpenditureRecordDto dto = new ExpenditureRecordDto();
            dto.setIds(ids);
            dto.setStatus(expenditure.getStatus());
            List<ExpenditureRecordDto> dtos = this.findRecordList(dto);
            for(ExpenditureDto exp : page.getList()) {
                if (dtos != null && dtos.size() > 0) {
                    for (ExpenditureRecordDto d : dtos) {
                        if(exp.getId().equals(d.getExpid())){
                            //状态不为空，设置公共字段内容   （审批人、审批时间等内容）
                            if(StringUtils.isNotEmpty(expenditure.getStatus())){
                                exp.setCreateName(d.getCreateName());
                                exp.setCreateTime(d.getCreateDate());
                            }
                            //审批类型
                            if(d.getStatus().equals("1")){
                                exp.setToUser(d.getCreateName());
                                exp.setToDate(d.getCreateDate());
                            }
                            //复核类型
                            if(d.getStatus().equals("2")){
                                exp.setRecheck(d.getCreateName());
                                exp.setRecheckdate(d.getCreateDate());
                            }
                            //经理审核类型
                            if(d.getStatus().equals("3")){
                                exp.setManagerName(d.getCreateName());
                                exp.setManagerDate(d.getCreateDate());
                            }
                            //出金类型
                            if(d.getStatus().equals("4")){
                                exp.setGold(d.getCreateName());
                                exp.setGoldDate(d.getCreateDate());
                            }
                        }
                    }
                }
            }

        }
        return page;
    }

    @Override
    public PageInfo<ExpenditureDto> findList(DataGrid grid,TbExpenditure expenditure) {
        grid.getOrderBy();
        return PageHelper.startPage(grid.getPageNum(), grid.getPageSize())
                .doSelectPageInfo(() -> this.tbExpenditureMapper.findList(expenditure));
    }
}
